﻿// Fill out your copyright notice in the Description page of Project Settings.

#include "SimpleGameMode.h"
#include "SimplePlayerState.h"
#include "SimpleGameState.h"
#include "SimplePlayerController.h"
#include "SimpleCharacter.h"
#include "Kismet/GameplayStatics.h"
#include "GameFramework/PlayerStart.h"
#include "UObject/ConstructorHelpers.h"
#include "Blueprint/AIBlueprintHelperLibrary.h"
#include "SimpleAIPawn.h"
#include "SimpleAIController.h"




ASimpleGameMode::ASimpleGameMode()
{
	BotNumber = 0;
	SpawnBotTime = 0;
	KillScore = 10.f;
	GameStartTime = 0;
	bCanUpdateBot = false;
}

void ASimpleGameMode::Killed(AController* Killer, AController* KilledPlayer)
{
	ASimplePlayerState* KillerPlayerState = Killer ? Cast<ASimplePlayerState>(Killer->PlayerState) : NULL;
	ASimplePlayerState* VictimPlayerState = KilledPlayer ? Cast<ASimplePlayerState>(KilledPlayer->PlayerState) : NULL;
	if (KillerPlayerState && KillerPlayerState != VictimPlayerState)
	{
		KillerPlayerState->ScoreKill(VictimPlayerState, KillScore);
	}
	if (VictimPlayerState)
	{
		VictimPlayerState->ScoreDeath(KillerPlayerState);
	}
}


void ASimpleGameMode::UpdateGame()
{
	GameStartTime++;

	/*TArray<AActor*> PlayerArr;
	UGameplayStatics::GetAllActorsOfClass(this, PlayerPawn, PlayerArr);
	bCanUpdateBot = PlayerArr.Num() > 0;*/

	int32 BotIndex = 0;

	
	
	if ((GameStartTime % SpawnBotTime == 0) && BotNumber > 0)
	{
		TArray<class AActor*> OutBotArr;
		UGameplayStatics::GetAllActorsOfClass(this, AIPawn, OutBotArr);
		BotIndex = OutBotArr.Num();
		if (BotIndex < BotNumber)
		{
			SpawnBot(OutAPlayerStartArr[FMath::RandHelper(OutAPlayerStartArr.Num())]->GetActorTransform());
			GameStartTime = GameStartTime > 3600 ? 0 : GameStartTime;
		}
	}
	/*if (!(GameStartTime % AIUpdateTime) && bCanUpdateBot)
	{
		TArray<class AActor*> OutBotArr;
		UGameplayStatics::GetAllActorsOfClass(this, AIPawn, OutBotArr);
		if (OutBotArr.Num()>0)
		{
			for (int32 i = 0; i < OutBotArr.Num(); i++)
			{
				ASimpleAIPawn* Bot = Cast<ASimpleAIPawn>(OutBotArr[i]);
				if (Bot)
				{
					ASimpleAIController* BotController = Cast<ASimpleAIController>(Bot->GetController());
					BotController->UpdateAI();
				}
			}
		}
	}*/
}

void ASimpleGameMode::SpawnBot(FTransform SpawnTransform)
{
	if (BehaviorTree && AIPawn)
	{
		//int32 AIUpdateCount = GameStartTime / AIUpdateTime;
		ASimpleAIPawn* AIBot = Cast<ASimpleAIPawn>(UAIBlueprintHelperLibrary::SpawnAIFromClass(this, AIPawn, BehaviorTree, SpawnTransform.GetLocation(), SpawnTransform.Rotator(), true));
		/*if (AIBot && AIUpdateCount > 0 && bCanUpdateBot)
		{
			for (int32 i=0;i<AIUpdateCount;i++)
			{
				ASimpleAIController* AIBotController = Cast<ASimpleAIController>(AIBot->GetController());
				if (AIBotController)
				{
					AIBotController->UpdateAI();
				}
			}
		}*/
	}
}

void ASimpleGameMode::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
	GetWorldTimerManager().ClearTimer(TimerHandle_GameDefultTime);
	Super::EndPlay(EndPlayReason);
}

void ASimpleGameMode::BeginPlay()
{
	Super::BeginPlay();
	GetWorldTimerManager().SetTimer(TimerHandle_GameDefultTime, this, &ASimpleGameMode::UpdateGame, 1.f, true);
	UGameplayStatics::GetAllActorsOfClass(this, BotPlayerStart, OutAPlayerStartArr);
}

